package indi.mofan.a13;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

public class AutoProxyTest {

    public static void main(String[] args) {

//        System.getProperties().put("sun.misc.ProxyGenerator.saveGeneratedFiles", "true");
        System.getProperties().put("jdk.proxy.ProxyGenerator.saveGeneratedFiles", "true");

        A13.Target target = new A13.Target();
        A13.Foo fooProxy = (A13.Foo) Proxy.newProxyInstance(AutoProxyTest.class.getClassLoader(), new Class[]{A13.Foo.class}, new InvocationHandler() {
            @Override
            public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
                // 1. 功能增强
                System.out.println("before...");
                // 2. 调用目标
                return method.invoke(target, args);
            }
        });

        fooProxy.foo();
        // 调用另一个方法
        System.out.println(fooProxy.bar());
    }
}
